use bevy::{sprite::Mesh2dHandle, window::WindowResized};

use super::*;
pub fn resize(
    mut window_resize: EventReader<WindowResized>,
    mut window_size: ResMut<WindowSize>,
    mut block_size: ResMut<BlockSize>,
    mut query_transform: Query<(&mut Transform, &Block)>,
    mut query_sprite: Query<&mut Sprite>,
    mut query_mesh: Query<&mut Mesh2dHandle>,
    mut meshs: ResMut<Assets<Mesh>>,
) {
    if let Some(window) = window_resize.read().last() {
        *block_size = BlockSize {
            width: f32::min(window.width / SIZE.0 as f32, window.height / SIZE.1 as f32),
        };
        *window_size = WindowSize {
            width: SIZE.0 as f32 * block_size.width,
            height: SIZE.1 as f32 * block_size.width,
        };
        query_transform.for_each_mut(|(mut transforma, block)| {
            transforma.translation = to_translation(
                &window_size,
                &block_size,
                block.location.0,
                block.location.1,
            )
        });
        query_sprite.for_each_mut(|mut sprite| {
            sprite.custom_size = Some(Vec2::new(block_size.width, block_size.width));
        });
        *meshs
            .get_mut(query_mesh.get_single_mut().unwrap().0.id())
            .unwrap() = Mesh::from(shape::Circle::new(block_size.width / 2.));
    }
}
